package com.lesso.cyg.sunflower.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.lesso.cyg.sunflower.core.ApiResponse;
import com.lesso.cyg.sunflower.pojo.dto.EmployeeDTO;
import com.lesso.cyg.sunflower.pojo.dto.EmployeeQueryDTO;
import com.lesso.cyg.sunflower.pojo.dto.EmployeeStatusDTO;
import com.lesso.cyg.sunflower.service.IEmployeeService;
import com.lesso.cyg.sunflower.pojo.vo.EmployeeVO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 员工基础信息 前端控制器
 * </p>
 *
 * @author cyg
 * @since 2025-07-02
 */
@RestController
@RequestMapping("/base/employee")
public class EmployeeController {
    @DubboReference
    private IEmployeeService employeeService;

    private final StringRedisTemplate stringRedisTemplate;
    private final AmqpTemplate amqpTemplate;

    public EmployeeController(StringRedisTemplate stringRedisTemplate, AmqpTemplate amqpTemplate) {
        this.stringRedisTemplate = stringRedisTemplate;
        this.amqpTemplate = amqpTemplate;
    }
    @GetMapping("/redis")
    public String getRedis(@RequestParam("key") String key) {
        return ApiResponse.<String>ok().data(stringRedisTemplate.opsForValue().get(key)).build();
    }

    @PostMapping("/redis")
    public ApiResponse<String> setRedis(@RequestParam("key") String key, @RequestParam("value") String value) {
        stringRedisTemplate.opsForValue().set(key, value);
        return ApiResponse.<String>ok().build();
    }

    @PostMapping
    public ApiResponse<Integer> save(@Valid @RequestBody EmployeeDTO dto) {
        return ApiResponse.ok("success",employeeService.save(dto));
    }

    @GetMapping("/page")
    public ApiResponse<Page<EmployeeVO>> page(EmployeeQueryDTO dto) {
        return ApiResponse.ok("success", employeeService.page(dto));
    }

    @GetMapping("/custom/page")
    public ApiResponse<Page<EmployeeVO>> customPage(EmployeeQueryDTO dto) {
        return ApiResponse.ok("success", employeeService.customPage(dto));
    }

    @DeleteMapping("/{id}")
    public ApiResponse<Object> delete(@PathVariable("id") Long id) {
        Assert.notNull(id, "id不能为空");
        if (id<0){
            throw new RuntimeException("id不能为空");
        }
        employeeService.deleteById(id);
        return ApiResponse.ok();
    }

    @PutMapping
    public ApiResponse<Integer> update(@Valid @RequestBody EmployeeDTO dto) {
        return ApiResponse.ok("success", employeeService.update(dto));
    }

    // 添加修改员工状态的接口
    @PutMapping("/status")
    public ApiResponse<Integer> updateEmploymentStatus(@Valid @RequestBody EmployeeStatusDTO dto) {
        return ApiResponse.ok("success", employeeService.updateEmploymentStatus(dto));
    }
}